package com.xiaoyao.novel.action.page;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionContext;
import com.xiaoyao.novel.common.Constant;
import com.xiaoyao.novel.pojo.Admin;
import com.xiaoyao.novel.pojo.WebInfo;
import com.xiaoyao.novel.service.AdminService;
import com.xiaoyao.novel.service.WebInfoService;
 
public class LoginAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private AdminService adminService = new AdminService();
	WebInfoService webInfoService=new WebInfoService();
	private static Logger logger = Logger.getLogger(LoginAction.class);
	private Admin admin;

	@Override
	public String execute() throws Exception {
		// 处理数据
		Admin retAdmin = adminService.login(admin);
		logger.info("retAdmin:" + retAdmin);
		if (retAdmin == null) {
			request.setAttribute("msg", "用户名或密码错误");
			return INPUT;
		}
		WebInfo webInfo=webInfoService.getWebInfo();
		logger.info("webInfo:" + webInfo);
		session.setAttribute(Constant.ADMIN_SESSION_KEY, retAdmin); 
		ActionContext.getContext().getApplication().put("webInfo", webInfo);
		ActionContext.getContext().getApplication().put("admin", retAdmin);
		return SUCCESS;
	}

	public String login() {
		return INPUT;
	}

	public String exit() {
		// 记录日志
		Admin loginAdmin = (Admin) session.getAttribute(Constant.ADMIN_SESSION_KEY);
		if (loginAdmin == null) {
			return INPUT;
		}
		session.removeAttribute(Constant.ADMIN_SESSION_KEY);
		return INPUT;
	}

	// ////////////////////////////
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
}
